"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

require("core-js/modules/es.array.concat");

require("core-js/modules/es.array.index-of");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.regexp.exec");

require("core-js/modules/es.regexp.to-string");

require("core-js/modules/es.string.split");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

/*
 * Copyright (C) 2016 Bilibili. All Rights Reserved.
 *
 * @author zheng qian <xqq@xqq.im>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var ParamSeekHandler = /*#__PURE__*/function () {
  function ParamSeekHandler(paramStart, paramEnd) {
    (0, _classCallCheck2.default)(this, ParamSeekHandler);
    this._startName = paramStart;
    this._endName = paramEnd;
  }

  (0, _createClass2.default)(ParamSeekHandler, [{
    key: "getConfig",
    value: function getConfig(baseUrl, range) {
      var url = baseUrl;

      if (range.from !== 0 || range.to !== -1) {
        var needAnd = true;

        if (url.indexOf('?') === -1) {
          url += '?';
          needAnd = false;
        }

        if (needAnd) {
          url += '&';
        }

        url += "".concat(this._startName, "=").concat(range.from.toString());

        if (range.to !== -1) {
          url += "&".concat(this._endName, "=").concat(range.to.toString());
        }
      }

      return {
        url: url,
        headers: {}
      };
    }
  }, {
    key: "removeURLParameters",
    value: function removeURLParameters(seekedURL) {
      var baseURL = seekedURL.split('?')[0];
      var params = undefined;
      var queryIndex = seekedURL.indexOf('?');

      if (queryIndex !== -1) {
        params = seekedURL.substring(queryIndex + 1);
      }

      var resultParams = '';

      if (params != undefined && params.length > 0) {
        var pairs = params.split('&');

        for (var i = 0; i < pairs.length; i++) {
          var pair = pairs[i].split('=');
          var requireAnd = i > 0;

          if (pair[0] !== this._startName && pair[0] !== this._endName) {
            if (requireAnd) {
              resultParams += '&';
            }

            resultParams += pairs[i];
          }
        }
      }

      return resultParams.length === 0 ? baseURL : baseURL + '?' + resultParams;
    }
  }]);
  return ParamSeekHandler;
}();

var _default = ParamSeekHandler;
exports.default = _default;